const api = require("../../../utils/marketAPIUtils");
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressInfo: {},
  },

  bindInput(e){
    console.log(e);
    let name = e.currentTarget.dataset.name;
    let addressInfo = this.data.addressInfo;
    addressInfo[name] =  e.detail.value;
    this.setData({
        addressInfo:addressInfo
    })
  },

  bindChecked(e) {
    this.data.addressInfo.isDefault = !this.data.checked
    this.setData({
      checked: !this.data.checked
    })
  },

  btnPicker(e){
    const data = e.detail.value;
    const addressInfo = this.data.addressInfo;
    addressInfo.province=data[0];
    addressInfo.city=data[1];
    addressInfo.cityArea=data[2];
    this.setData({addressInfo:addressInfo})
  },

  btnSubmit:function (e) {
      console.log(e);
    wx.showLoading({
      title: '提交中',
    })
    api.post(api.url.saveAddress, this.data.addressInfo, function (res) {
      console.log(res.data);
      wx.hideLoading({
        success: (res) => {},
      })
      if (res.data.code == 200) {
        wx.showToast({
          title: '成功',
          icon: 'success',
          duration: 2000,
          complete: function () {
            // 返回时刷新上个页面
            let pages =getCurrentPages()
            let perPage =pages[pages.length -2]
            perPage.onLoad()
            wx.navigateBack();
          }
        })
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'error',
          duration: 2000,
        })
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})